package cn.lingyangwl.agile.infra.gen.manager;

import cn.hutool.core.io.FileUtil;
import cn.lingyangwl.agile.infra.gen.model.WordDataUnderTitle;
import cn.lingyangwl.framework.tool.core.exception.BizException;
import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 文件表格管理者
 * @author shenguangyang
 */
@Slf4j
public abstract class WordTableManager {
    protected static final Map<WordFileType, WordTableManager> wordTableManagerMap = new ConcurrentHashMap<>();

    public static WordTableManager getInstance(String filePath) {
        String mimeType = FileUtil.getMimeType(filePath);
        WordFileType type = null;
        if ("application/msword".equals(mimeType)) {
            type = WordFileType.DOC;
        } else if ("application/vnd.openxmlformats-officedocument.wordprocessingml.document".equalsIgnoreCase(mimeType)) {
            type = WordFileType.DOCX;
        }

        if (!wordTableManagerMap.containsKey(type)) {
            throw new BizException("暂时不支持文件 mimeType: {}", mimeType);
        }
        log.info("mimeType: {}", mimeType);
        return wordTableManagerMap.get(type);
    }

    public abstract List<WordDataUnderTitle> getWordDataList(String path) throws IOException;
}
